package com.jhws.business.advertisement.bo;

import com.jhws.business.advertisement.service.ITblNoticeDimService;
import com.jhws.core.dataUpdate.bo.DataUpdateObject;
import com.jhws.core.dataUpdate.bo.IDataUpdateModule;
import com.jhws.core.dataUpdate.service.DataUpdateService;
import com.jhws.util.DateUtil;
import com.jhws.util.LogHelper;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class NoticeUpdateData implements IDataUpdateModule{

    static public String NOTICE = "notice";
    private String [] name = {NOTICE};

    @Resource
    DataUpdateService dataUpdateService;

    @Resource
    ITblNoticeDimService INoticeService;


    @Override
    public String [] getModuleName() {
        return name;
    }

    @Override
    public DataUpdateObject getUserModuleData(String username, String ownerCode,Long timeStamp, String module, Map<String, String> moduleParams) throws Exception {
        String room = moduleParams.get("room");
        if(timeStamp == null || room == null)
            return null;

        String where = "where 1=1 and timestamp > '"+timeStamp+"'";

        where += " and equ_type = '"+room+"'";

        List<TblNoticeDimBo> list = INoticeService.findTblNoticeDimByWhere(where);
        return new DataUpdateObject(list);
    }

    @Override
    public DataUpdateObject getLabelModuleData(String commCode, Long timeStamp, String module, Map<String,String> moduleParams) throws Exception {
        if(timeStamp == null)
            return null;

        String where = "where 1=1 and timestamp > '"+timeStamp+"'";

        if(moduleParams.get("type") !=null && moduleParams.get("type").equals("outdoor")) {
            String timeStr = DateUtil.getTimeNowFmtDay();
            where += " and begin_date <= '"+timeStr+"' and end_date >= '"+timeStr+"'";
        }

        if(StringUtils.isNotEmpty(commCode))
            where += " and comm_code = '"+commCode+"' and equ_type = '"+ commCode +"'";

        List<TblNoticeDimBo> list = INoticeService.findTblNoticeDimByWhere(where);
        LogHelper.t("sql : "+where +" size "+(list == null?0:list.size()));
        return new DataUpdateObject(list);
    }

    @Override
    @PostConstruct
    public void registerModule() {
        dataUpdateService.registerModule(this);
    }
}
